<template>
  <div>
    <customHeader :middleTitle="title" />
  </div>
</template>

<script>
import Vue from "vue";
import customHeader from "../components/header/header";
import { Toast, MessageBox } from "mint-ui";
import jsBridge from "../tools/jsBridge.js";
export default {
  name: "scanResult",
  data() {
    return {
      scanResult: "",//https://mp.zonghengke.com/html/order.html?c=181010040000145932&s=20190508100225523497
      title: "识别中..."
    };
  },
  methods: {
    scanRequest() {
      const url = `newapp/voice/scanQr.do?scanResult=${encodeURIComponent(
        this.scanResult
      )}`;
      this.ajax({
        url: url,
        type: "get",
        controlLoading: true
      })
        .then(result => {
          this.title = "识别结果";
          if (result.success) {
            const res = result.resultObject;

            if (res.action == 1) {
              //码卡龙配网
              this.$router.replace({
                path: "/qrCodeSound/mainSettings/networkSetting",
                query: { sn: res.sn, type: res.type, shopId: res.shopId, close: "true" }
              });
            } else if (res.action == 2) {
              //绑定门店
              sessionStorageClient.putJSON("deviceStoreList", res.shopLists); //将门店列表存储，绑定门店页面使用
              this.$router.replace({
                path: "/qrCodeSound/bindingStore",
                query: {
                  sn: res.sn,
                  type: res.type,
                  close: "true"
                }
              });
            } else if (res.action == 3) {
              //开通进件
              this.$router.replace({
                path: "/qrCodeSound/authNone",
                query: {
                  className: "mkl-unopen",
                  shopId: res.shopId,
                  sn: res.sn,
                  type: res.type,
                  close: "true"
                }
              });
            } else if (res.action == 4) {
              //主设置面板
              const data = res["data"];
              this.$router.replace({
                path: "/qrCodeSound/mainSettings",
                query: {
                  sn: data.sn,
                  type: data.type,
                  shopId: data.shopId,
                  close: "true"
                }
              });
            } else if (res.action == 5) {
              //扫进件二维码（状态为进件中 跳转到门店管理页面）
              this.$router.replace({
                path: "/storeManage"
              });
            }
          } else if (result.code == -2) {
            //没有权限
            MessageBox({
              title: "",
              message: "没有权限",
              showCancelButton: false,
              confirmButtonText: "知道了",
              closeOnClickModal: false
            }).then(action => {
              if (action == "confirm") {
                jsBridge.closeWindow();
              }
            });
          } else {
            MessageBox({
              title: "",
              message: result.msg,
              showCancelButton: false,
              confirmButtonText: "知道了",
              closeOnClickModal: false
            }).then(action => {
              if (action == "confirm") {
                jsBridge.closeWindow();
              }
            });
          }
        })
        .catch(err => {
          this.title = "识别结果";
          MessageBox({
            title: "",
            message: "扫码服务错误",
            showCancelButton: false,
            confirmButtonText: "知道了",
            closeOnClickModal: false
          }).then(action => {
            if (action == "confirm") {
              jsBridge.closeWindow();
            }
          });
        });
    },
  },
  created() {
    const decodeHash = decodeURIComponent(window.location.hash);
    if (decodeHash && decodeHash.split("scanResult=").length > 1) {
      this.scanResult = decodeHash.split("scanResult=")[1];
    } else {
      MessageBox({
        title: "",
        message: "二维码信息解析错误",
        showCancelButton: false,
        confirmButtonText: "知道了",
        closeOnClickModal: false
      }).then(action => {
        if (action == "confirm") {
          jsBridge.closeWindow();
        }
      });
      return;
    }
    if (this.scanResult.indexOf("order.html") > -1) {//订单详情二维码（通过扫码进入订单详情）
      const clientOrderId = window.CommonUtil.getUrlParam3("c",this.scanResult);
      if (!clientOrderId) {
        Toast('缺少订单Id');
        return;
      } else {
        const shopId = window.CommonUtil.getUrlParam3("s",this.scanResult);
        this.$router.replace({ path: "/orderDetails", query: { clientOrderId: clientOrderId, shopId: shopId, close: "true" } });
      }
    } else {
      this.scanRequest();//机具设备二维码
    }
  },
  components: {
    customHeader
  }
};
</script>
